<?php

namespace XWAM\Model;

/**
 * DocumentCategoryModel 类
 *
 * @since 1.0 <2015-10-24> SoChishun <14507247@qq.com> Added.
 */
class DocumentCategoryContentModel extends AppbaseModel {

    protected $tableName = 't_porg_document_category_content';
    protected $pk = 'category_id';
    protected $patchValidate = true;

    function save_content() {
        $rules = array(
            array('content', 'require', '内容无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        );
        if (!$this->validate($rules)->create()) {
            return $this->returnMsg(false, $this->getError());
        }
        $action = $this->data['action'];
        unset($this->data['action']);
        if ('add' == $action) {
            $result = $this->add();
        } else {
            $result = $this->save();
        }
        return $this->returnMsg($result);
    }

    /**
     * 获取内容
     * @param string $code
     * @param boolean $decode
     * @param string $field
     * @return array
     * @since 1.0 2016-8-31 SoChishun Added.
     */
    function get_item_by_code($code, $decode = true, $field = 'id, title') {
        if (false === strpos($field, 'id,')) {
            $field = 'id, ' . $field; // 确保包含id字段
        }
        $data = $this->table('t_porg_document_category')->field($field)->where(array('code' => $code))->find();
        if (!$data) {
            return false;
        }
        $content = $this->where(array('category_id' => $data['id']))->getField('content');
        if ($decode && $content) {
            $content = html_entity_decode($content);
        }
        $data['content'] = $content ? $content : '';
        return $data;
    }

}
